﻿
    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    
    var domain = getParameterByName('domain');
    var index = parseInt(getParameterByName('index'));
    var time = parseInt(getParameterByName('time'));
    
    if (isNaN(index)) {
        index = 0;
    }
                
    if (time != null) {
        if (isNaN(time)) {
            time = 15;
        }
        document.getElementById('time').value = time;
    }

    document.getElementById('domain').value = domain;

    var error = '<div class="alert alert-danger" role="alert">错误！网址不能为空！请输入正确的网址！</div>';
    
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.addEventListener("load", transferComplete, false);
    xmlhttp.addEventListener("error", transferFailed, false);
    xmlhttp.open("GET", "urls.txt", true);
    xmlhttp.send();
    
    function transferComplete() {

        var urlTxt = xmlhttp.responseText.match(/.+/g);
        var len = parseInt(urlTxt.length);

        var isDone = (index+1) >= len;

        if (isDone) {
        
            if (! domain=="") {
                document.getElementById("timenum").innerHTML = '外链推广已完成。您可以<a href="http://seo.puamac.cn/">点此返回首页</a>或输入域名开启新的一轮工作！';
                document.getElementById("success").innerHTML='<div class="alert alert-success" role="alert">恭喜！'+domain+' 的自动化外链推广工作已经完成！总共发布了'+len+'条外链。（更多网址正在筛选中，敬请期待！收藏本站快捷键：CTRL+D）</div>';
            } else {
                document.getElementById("success").innerHTML = error;
            }

        } else {
        
            if (! domain=="") {

                document.title = '正在群发外链...';
                
                var nextIndex = index + Math.min(10, (len-index));
                
                var restmin = Math.ceil(Math.ceil((len-nextIndex)/10)*time / 60);
                
                //定时跳转
                window.countDown = function(secs){
                    
                    var restminHTML = restmin > 0 ? '预计剩余所需时间' + restmin +'分钟' : '剩余所需时间已不足一分钟';
                
                    timenum.innerHTML = '<p class="small">本次任务共计' + len + '条目标外链，已发布' + index + '条外链，'+restminHTML+'。</p><p>「 ' + secs + ' 」秒后自动跳转到下一页…</p>';
                    if (--secs > 0){
                        setTimeout("countDown("+secs+")",1000);
                    }else{
                        window.location.href = "/index.html?domain="+domain+"&index="+nextIndex+"&time="+time;
                    }
                }
                countDown(time);
            
                document.getElementById("submit-input").innerHTML = '<button type="button" class="btn btn-primary" onclick="window.location.href=\'http://seo.puamac.cn/\';">停止工作</button>';

                document.getElementById("success").innerHTML = '<div class="alert alert-success" role="alert">好了，自动化外链推广工作已经开始，现在缩小窗口去做别的事吧！</div>';

                var jobHTML = '<ol>';

                for (var i=index; i<nextIndex; i++){
                
                    var targetUrl = urlTxt[i].replace("#domain#", domain);
                    // jobHTML += '<li><a rel="nofollow" href="'+targetUrl+'" target="_blank">推广 '+domain+'</a></li>';
                
                    jobHTML += '<li><a rel="nofollow" href="'+targetUrl+'" target="_blank">推广 '+domain+'</a></li><iframe rel="nofollow" src="tool.html#'+targetUrl+'" height="60" width="90%" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" style="margin-top:10px"></iframe>';
                }
                jobHTML += '</ol>';

                document.getElementById("jobs").innerHTML = jobHTML;

            } else {
                document.getElementById("success").innerHTML = error;
            }
        }
    }
    
    function transferFailed() {
        document.getElementById("success").innerHTML = '<div class="alert alert-danger" role="alert">出错了，推广地址获取失败。</div>';
    }